


#ifndef WINDOW_H
#define WINDOW_H


#include <QWidget>
#include <QTransform>
#include <QString>
#include <QList>
#include <QRegion>
#include <QVector>
#include <QtGui>
#include <QtCore>
class QLabel;
class QDoubleSpinBox;


#include "gnetudp.h"
#include "ui_window.h"


struct Weight
{
	quint32 p1;
	quint16 w1;
	quint32 p2;
	quint16 w2;
	quint32 p3;
	quint16 w3;
	quint32 p4;
	quint16 w4;
};


// --------------------- GImageTransformation{} ------------------------------
// ---------------------------------------------------------------------------
class GImageTransformation
{
public:
	enum Format
	{
		Format_Indexed8,   ///< The image is stored using 8-bit indexes into a colormap.
		Format_RGB24       ///< The image is stored using a 24-bit RGB format (0xRRGGBB).
	};

	enum Interpolation
	{
		Interpolation_NN,        ///< The nearest neighbour interpolation.
		Interpolation_Bilinear   ///< Bilinear interpolation.
	};
	
	// ----------------------------------------------------
	// constructors/destructors 
	
		/// Creates empty transformation. 
	GImageTransformation();
	
	
	// ----------------------------------------------------
	// setup
	
		/// Returns size of transformation.
	QSize outputSize()const { return _outputSize; }	
		/// Returns width of transformation. 
	int outputWidth()const { return _outputSize.width(); }
		/// Returns height of transformation 
	int outputHeight()const { return _outputSize.height(); }
	
		/// Returns size of transformation.
	QSize inputSize()const { return _inputSize; }	
		/// Returns width of transformation. 
	int inputWidth()const { return _inputSize.width(); }
		/// Returns height of transformation 
	int inputHeight()const { return _inputSize.height(); }
	
		/// Resizes transformation with the given \a size.
		/// Resized transformation is emtpy. 
	void setOutputSize( const QSize & outputSize );
	
	void setInputSize( const QSize & inputSize ) { _inputSize = inputSize; }
	
	void setInterpolation( const Interpolation interpolation );
	
	void setWeight( const int index, Weight weight ) { _weights[index] = weight; }
	
	// ----------------------------------------------------
	// actions 
	
		/// Clears all points in \e _pixels. Keeps transformation size.
	void clear();
	
	QImage transform( const QImage & source );
	
		/// Transforms \a source image with data in \e _pixels.
	void transform( int sw, int sh, const char * pSource,
					int dw, int dh, char * pDest,
					Format format = Format_Indexed8 );
	
	quint32 & operator [] ( const int index );
	
protected:
	void transformFormat_Indexed8( int sw, int sh, const char * pSource,
								   int dw, int dh, char * pDest );
	void transformFormat_RGB24( int sw, int sh, const char * pSource,
								int dw, int dh, char * pDest );
	
private:
		/// represents a transformation of image, pixel by pixel
		/// _pixels consists of columns, column is a QVector< QPoint >
	QVector< quint32 >  _pixels;
	QVector< Weight >  _weights;
	
	QSize _inputSize;
	QSize _outputSize; ///< size of transformation
	
	Interpolation _interpolation;
	
}; // class !GImageTransformation
// ---------------------------------------------------------------------------











// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
class Window : public QWidget, protected Ui::Window
{
	Q_OBJECT

public:
	Window( QWidget * pParent = 0, Qt::WindowFlags f = 0 );
	~Window() {}
	
	QString toString( QTransform & transformation );

	
	void Log( const QString& text );

protected slots:
	virtual void on_pushReceive_toggled( bool b );
	virtual void on_buttonSave_clicked();
	virtual void on_pushOpen_clicked();
	
	void udpPacketReceived( int packetId, const QByteArray& data );
	void udpPacketsReceived( const QQueue< QPair< quint16, QByteArray> >& packets );
	
	void createTransformation();
	void transform();
	
private:
	void dataReaded();
	QByteArray _data;		///< Buffer for incoming data
	//quint64 _bytesToRead;	///< Byters left to read

	QImage _sourceImage;
	QImage _resultImage;
	
	GImageTransformation _transformation;
	GUdpReceiver	_udpReceiver; ///< Udp receiver
	
}; // class !Window
// ---------------------------------------------------------------------------


#endif // !WINDOW_H


// eof
